//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.mass.core.activiti.workflow.controller;

import com.mass.core.framework.aop.LogAop;
import com.mass.core.utils.AjaxResponse;

import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@RestController
@RequestMapping({"/activiti/workflow/processinstance"})
public class ProcessInstanceController {
    @Autowired
    private RuntimeService runtimeService;

    public ProcessInstanceController() {
    }

    @LogAop(menuName = "流程实例管理",operationDesc = "查询流程列表")
    @RequestMapping(
        value = {"/running"},
        method = {RequestMethod.GET}
    )
    public AjaxResponse running(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
        pageIndex = Integer.valueOf(pageIndex == null?1:pageIndex.intValue());
        pageSize = Integer.valueOf(pageSize == null?10:pageSize.intValue());
        ProcessInstanceQuery processInstanceQuery = this.runtimeService.createProcessInstanceQuery();
        List<?> list = processInstanceQuery.listPage((pageIndex-1)*pageSize,pageSize);
        return AjaxResponse.success("ok", list);
    }

    @RequestMapping({"update/{state}/{processInstanceId}"})
    public String updateState(@PathVariable("state") String state, @PathVariable("processInstanceId") String processInstanceId, RedirectAttributes redirectAttributes) {
        if(state.equals("active")) {
            redirectAttributes.addFlashAttribute("message", "已激活ID为[" + processInstanceId + "]的流程实例。");
            this.runtimeService.activateProcessInstanceById(processInstanceId);
        } else if(state.equals("suspend")) {
            this.runtimeService.suspendProcessInstanceById(processInstanceId);
            redirectAttributes.addFlashAttribute("message", "已挂起ID为[" + processInstanceId + "]的流程实例。");
        }

        return "redirect:/workflow/processinstance/running";
    }
}
